﻿namespace TomatoBreak.ExtensionMethods
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;

    public static class DictionaryExtensions
    {
        [DebuggerStepThrough]
        public static void Merge<TKey, TValue>(
            this IDictionary<TKey, TValue> instance,
            IEnumerable<KeyValuePair<TKey, TValue>> source,
            bool replaceExisting = false)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            foreach (var pair in source.Where(pair =>
                replaceExisting || !instance.ContainsKey(pair.Key)))
            {
                instance[pair.Key] = pair.Value;
            }
        }
    }
}